package hikari.objects3d.light;

import com.gooddies.wiring.Wiring;
import hikari.gui.panels.LightPanel;
import hikari.hcomponents.HComponent;
import hikari.managers.Manager3D;
import hikari.objects3d.Basic3dObject;
import hikari.persistence.file.FileSection;
import java.io.IOException;
import java.util.List;
import org.swig.jirr.E_LIGHT_TYPE;
import org.swig.jirr.E_MATERIAL_TYPE;
import org.swig.jirr.IBillboardSceneNode;
import org.swig.jirr.ILightSceneNode;
import org.swig.jirr.ISceneManager;
import org.swig.jirr.ITexture;
import org.swig.jirr.ITriangleSelector;
import org.swig.jirr.MATERIAL_FLAG;
import org.swig.jirr.SLight;
import org.swig.jirr.dimension2df;

/**
 * @author Dmitry
 */
public abstract class Light3dObject extends Basic3dObject {

    private ILightSceneNode light;
    private E_LIGHT_TYPE lightType;
    private IBillboardSceneNode lightBillboard;

    public Light3dObject(ISceneManager sceneManager, E_LIGHT_TYPE lightType, String name) {
        super(name, true);
        this.lightType = lightType;
        initObject(sceneManager);
    }

    public ITexture getLightBillboardTexture() {
        return getBillboardTexture();
    }

    public Light3dObject(String name, ISceneManager sceneManager) {
        super(name);
        initObject(sceneManager);
    }

    public IBillboardSceneNode getBillboard() {
        return lightBillboard;
    }

    private void initObject(ISceneManager sceneManager) {
        Wiring.getComponent(Manager3D.class).removeDefaultLight();
        lightBillboard = sceneManager.addBillboardSceneNode(sceneManager.getRootSceneNode(), new dimension2df(2f, 2f));
        light = sceneManager.addLightSceneNode();
        light.setLightType(lightType);
        lightBillboard.setMaterialTexture(0, getLightBillboardTexture());
        lightBillboard.setMaterialType(E_MATERIAL_TYPE.TRANSPARENT_ALPHA_CHANNEL_REF);
        lightBillboard.setMaterialFlag(MATERIAL_FLAG.EMF_LIGHTING, false);
        lightBillboard.setMaterialFlag(MATERIAL_FLAG.EMF_USE_MIP_MAPS, false);
        lightBillboard.setMaterialFlag(MATERIAL_FLAG.EMF_ZBUFFER, false);
        Wiring.getComponent(Manager3D.class).addAlwaysVisibleBillboard(lightBillboard);
        light.setParent(lightBillboard);
        ITriangleSelector selector = sceneManager.createTriangleSelectorFromBoundingBox(lightBillboard);
        lightBillboard.setTriangleSelector(selector);
        setMeshSceneNode(lightBillboard);
    }

    public ILightSceneNode getLightNode() {
        return light;
    }

    @Override
    public List<HComponent> getEditPanels() {
        List<HComponent> components = super.getEditPanels();
        components.add(Wiring.getComponent(LightPanel.class).initialUpdateFromModel(this));
        return components;
    }

    @Override
    public boolean onRemove() {
        Wiring.getComponent(Manager3D.class).removeAlwaysVisibleBillboard(lightBillboard);
        return true;
    }

    public abstract void drawGizmos();

    protected abstract ITexture getBillboardTexture();

    @Override
    public void writeData(FileSection section) throws IOException {
        super.writeData(section);
        FileSection s = section.createNewSection("lightSetting");
        SLight data = getLightNode().getLightData();
        s.writeFloat("rad", data.getRadius());
        s.writeFloat("outerCone", data.getOuterCone());
        s.writeFloat("innerCone", data.getInnerCone());
        s.writeFloat("falloff", data.getFalloff());
        s.writeSColor("amb", data.getAmbientColor().toSColor());
        s.writeSColor("spec", data.getSpecularColor().toSColor());
        s.writeSColor("diff", data.getDiffuseColor().toSColor());
        s.close();
    }
}